/* 
 * Since 2008 - 2012
 */
package com.hh.bss.project.controller;

import static com.hh.bss.util.extjs.JsonHelper.outJson;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.hh.bss.base.*;
import com.hh.bss.common.page.*;
import com.hh.bss.project.entity.*;
import com.hh.bss.project.service.*;
import com.hh.bss.project.vo.query.*;
import com.hh.bss.util.extjs.ExtJsPageHelper;
import com.hh.bss.util.extjs.ListRange;

import java.util.*;




@Controller
public class CustomerPartnerController extends BaseSpringController {
	// 默认多列排序,example: username desc,createTime asc
	protected static final String DEFAULT_SORT_COLUMNS = null;

	@Autowired
	private CustomerPartnerService customerPartnerService;

	java.lang.Integer id = null;
	private String[] items;

	public void setId(java.lang.Integer val) {
		this.id = val;
	}

	public void setItems(String[] items) {
		this.items = items;
	}

	/**
	 * ExtGrid使用 列表
	 * 
	 * @throws IOException
	 */
	public void list(HttpServletRequest request, HttpServletResponse response, CustomerPartnerQuery query) throws IOException {
		PageRequest<Map> pr = ExtJsPageHelper.createPageRequestForExtJs(request, DEFAULT_SORT_COLUMNS);
		Page page = customerPartnerService.findPage(query);

		List<CustomerPartner> customerPartnerlist = (List) page.getResult();
		ListRange<CustomerPartner> resultList = new ListRange<CustomerPartner>();
		resultList.setRows(customerPartnerlist);
		resultList.setTotal(page.getTotalCount());
		resultList.setMessage("ok");
		resultList.setSuccess(true);
		outJson(response, resultList);
	}

	/**
	 * extGrid保存
	 * 
	 * @throws IOException
	 */
	public void save(HttpServletRequest request, HttpServletResponse response, CustomerPartner customerPartner) throws IOException {
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			// 设置创建用户和创建时间
			com.hh.bss.auth.client.OnlineManager onlineManager = super.getOnlineManager(request, response);
			if (onlineManager != null) {
				com.hh.bss.auth.client.OnlineUser onlineUser = onlineManager.getOnlineUser();
				if (onlineUser != null && com.hh.bss.util.BeanUtils.isExistsProperty(CustomerPartner.class, "creator")) {
					try {
						com.hh.bss.util.BeanUtils.setProperty(customerPartner, "creator", onlineUser.getUsername());
					} catch (Exception ex) {
					}
				}
			}

			if (com.hh.bss.util.BeanUtils.isExistsProperty(CustomerPartner.class, "createDt")) {
				try {
					com.hh.bss.util.BeanUtils.setProperty(customerPartner, "createDt", new Date());
				} catch (Exception ex) {
				}
			}
			
			if (StringUtils.isNotEmpty(customerPartner.getRole())) {
				// 如果是 职位负责人 和 合作顾问 验证业绩
				if ("MC".equals(customerPartner.getRole()) || "PC".equals(customerPartner.getRole())) {
					if (!customerPartnerService.validateOfferPartner(customerPartner)) {
						logger.info("方法调用之中......");
						result.put("failure", true);
						result.put("msg", "职位负责人和合作顾问的业绩总和不得超过 100");
						outJson(response, result);
						return;
					}
					logger.info("方法调用完成.....");
				}
			}
			
			if("MC".equals(customerPartner.getRole())){
				if(customerPartnerService.isExistMCRoleByJobId(customerPartner.getApId())){
					result.put("failure", true);
					result.put("msg", "职位负责人只能有且一个");
					outJson(response,result);
					return;
				}
			}
			if(!customerPartnerService.checkSameRoleAndAccount(customerPartner)){
				customerPartnerService.save(customerPartner);
				result.put("success", true);
				result.put("msg", "添加成功!");
			}else{
				result.put("failure", true);
				result.put("msg", "该人选已添加!");
			}
			
			
		} catch (Exception e) {
			result.put("failure", true);
			result.put("msg", e.getMessage());
			e.printStackTrace();
		}
		outJson(response, result);
	}

	/**
	 * extGrid修改
	 * 
	 * @throws IOException
	 */
	public void update(HttpServletRequest request, HttpServletResponse response, CustomerPartner customerPartner) throws IOException {
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			if (StringUtils.isNotEmpty(customerPartner.getRole())) {
				// 如果是 职位负责人 和 合作顾问 验证业绩
				if ("MC".equals(customerPartner.getRole()) || "PC".equals(customerPartner.getRole())) {
					if (!customerPartnerService.validateOfferPartner(customerPartner)) {
						logger.info("方法调用之中......");
						result.put("failure", true);
						result.put("msg", "职位负责人和合作顾问的业绩总和不得超过 100");
						outJson(response, result);
						return;
					}
					logger.info("方法调用完成.....");
				}
			}

			CustomerPartner oldCustomerPartner = (CustomerPartner) customerPartnerService.getById(customerPartner.getId());
			logger.info("创建人:"+oldCustomerPartner.getCreator());

			bind(request, oldCustomerPartner);
			// 设置修改用户和修改时间
			com.hh.bss.auth.client.OnlineManager onlineManager = super.getOnlineManager(request, response);
			if (onlineManager != null) {
				com.hh.bss.auth.client.OnlineUser onlineUser = onlineManager.getOnlineUser();
				if (onlineUser != null && com.hh.bss.util.BeanUtils.isExistsProperty(CustomerPartner.class, "modifier")) {
					try {
						com.hh.bss.util.BeanUtils.setProperty(customerPartner, "modifier", onlineUser.getUsername());
					} catch (Exception ex) {
					}
				}
			}

			if (com.hh.bss.util.BeanUtils.isExistsProperty(CustomerPartner.class, "modifiedDt")) {
				try {
					com.hh.bss.util.BeanUtils.setProperty(customerPartner, "modifiedDt", new Date());
				} catch (Exception ex) {
				}
			}

			customerPartnerService.update(oldCustomerPartner);
			result.put("success", true);
			result.put("msg", "修改成功!");

		} catch (Exception e) {
			result.put("failure", true);
			result.put("msg", e.getMessage());
			e.printStackTrace();
		}
		outJson(response, result);
	}

	/**
	 * extGrid删除
	 * 
	 * @throws IOException
	 */
	public void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String ids = request.getParameter("ids");
		String[] idarray = ids.split(",");
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			for (int i = 0; i < idarray.length; i++) {
				java.lang.Integer id = new java.lang.Integer((String) idarray[i]);
				customerPartnerService.removeById(id);
			}
			result.put("success", true);
			result.put("msg", "删除成功");
		} catch (Exception e) {
			result.put("failure", true);
			result.put("msg", e.getMessage());
			e.printStackTrace();
		}
		outJson(response, result);
	}

}
